$(function() {
    $('#side-menu').metisMenu();
    $('[rel=tooltip]').tooltip();
    
    $(".input-group-btn .clearBtn").click(function () {
        $(this).parent().prev().val('');
        $(this).closest("form").submit();
    });
    
    $("a[role=tab]").click(function () {
        location.hash = $(this).attr("href") + "_";
    });
    
    initIntegerFields($(".integer"));
    
    initDatepicker();
    $(document).on('change', '.datepicker', function () {
        var reg = /^\d{1,2}.\d{1,2}.\d{4}$/;
        var date = $(this).val();
        var isValid = false;
        if (date.match(reg)) {
            var parts = date.split(".");
            var dt = new Date(parts[1] + "/" + parts[0] + "/" + parts[2]);
            if (dt.getMonth() == parts[1] - 1 && dt.getDate() == parts[0] && dt.getFullYear() == parts[2])
                isValid = true;
        }
        if (!isValid)
            $(this).val('');
    });
});

function initDatepicker() {
    $('.datepicker').datepicker({ format: 'dd.mm.yyyy' });
}

function initIntegerFields(element) {
    element.keypress(function (e) {
        if (e.which != 8 && e.which != 0 && e.which != 13 && (e.which < 48 || e.which > 57)) {
            return false;
        }
    });
}

//Loads the correct sidebar on window load,
//collapses the sidebar on window resize.
// Sets the min-height of #page-wrapper to window size
$(function() {
    $(window).bind("load resize", function() {
        topOffset = 131;
        width = (this.window.innerWidth > 0) ? this.window.innerWidth : this.screen.width;
        if (width < 768) {
            $('div.navbar-collapse').addClass('collapse')
            topOffset *= 2; // 2-row-menu
        } else {
            $('div.navbar-collapse').removeClass('collapse')
        }

        height = (this.window.innerHeight > 0) ? this.window.innerHeight : this.screen.height;
        height = height - topOffset;
        if (height < 1) height = 1;
        if (height > topOffset) {
            $("#page-wrapper").css("min-height", (height) + "px");
        }
    })
})

function htmlDecode(str) {
    return String(str)
            .replace('&amp;', '&')
            .replace('&quot;', '"')
            .replace('&#39;', '\'')
            .replace('&lt;', '<')
            .replace('&gt;', '>');
}

function ReadOnlyView() {
    $("body :input").prop("disabled", true);
    $(".returnDiv").removeClass("hide");
}

function ReadOnlyDiv(divId) {
    $("#" + divId + " :input").prop("disabled", true);
}

function goBack() {
    $(".returnDiv a")[0].click();
}

function setFocus(selector) {
    selector.focus();
    var strLen = selector.val().length;
    selector[0].setSelectionRange(strLen, strLen);
}

function clearForm(formSel) {
    if (formSel == undefined) formSel = $('body');
    $("input[type='text']", formSel).val('');
    $("input[type='checkbox']", formSel).removeAttr("checked").change();
    $('select', formSel).each( function () {
            $(this)[0].selectedIndex = 0;
        }
    );
}